package com.demo.proxy.dynamicproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class UserProxy {

	private Object target;

	public UserProxy(Object target) {
		this.target = target;

	}

	public Object newProxyInstance() {

		Object object = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(),
				new InvocationHandler() {

					@Override
					public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						// TODO Auto-generated method stub

						return null;
					}
				});

		return object;
	}

	public Object getProxyInstance() {

		Object proxy = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(),
				new InvocationHandler() {
					@Override
					public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						Object result;
						String name = method.getName();
						int i = method.getModifiers();
						String retval = Modifier.toString(i);
						Class<?>[] parameterTypes = method.getParameterTypes();
						Class<?> returnType = method.getReturnType();
						System.out.println("returnType:"+Arrays.asList(returnType).toString());
						for (Object o : args) {
							System.out.println("args");
							System.out.println(o);
						}
						for (Class<?> class1 : parameterTypes) {
							System.out.println("parameterTypes");
							System.out.println(class1);
						}
						System.out.println(retval);
						if ("select".equals(name)) {

							result = method.invoke(target, args);
						} else {
							System.out.println("before");
							result = method.invoke(target, args);
							Parameter[] parameters = method.getParameters();
							System.out.println("parameters:" + Arrays.asList(parameters).iterator());
							System.out.println("after");
						}
						return result;
					}
				});

		return proxy;

	}
}
